package com.zhuoyue.biz.core.command.impl;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.DuoCaiDeng0Faa0B02CmdDeal;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 多彩灯
 * @author Administrator
 *
 */
public class DuoCaiDeng0Faa0B02 implements Device {

	@Override
	public String getCommand(String controlParam) {
		System.out.println("");
		Map<String, String> map = (Map)JSONObject.parse(controlParam);

		String controlType = map.get("controlType");
		if(StringUtils.isEmpty(controlType)){
			return null;
		}

		String cmd = "";

		if(controlType.equals(DuoCaiDeng0Faa0B02CmdDeal.ControlType.CONTROL_TYPE_AIMING)){
			cmd = getAimingControlCmd(map);
		}else if(controlType.equals(DuoCaiDeng0Faa0B02CmdDeal.ControlType.CONTROL_TYPE_CLOSE)){
			cmd = getCloseControlCmd(map);
		}else if(controlType.equals(DuoCaiDeng0Faa0B02CmdDeal.ControlType.CONTROL_TYPE_COLOR)){
			cmd = getColorControlCmd(map);
		}else if(controlType.equals(DuoCaiDeng0Faa0B02CmdDeal.ControlType.CONTROL_TYPE_MODE)){
			cmd = getModeControlCmd(map);
		}

		return cmd;
	}

	/**
	 * 获取调光控制命令
	 * @param map
	 * @return
	 */
	private String getAimingControlCmd(Map<String, String> map){

		Map<String, String> actionsMap = (Map)JSONObject.parse(JSON.toJSONString(map.get("actions")));

		String cmd10To15 = DuoCaiDeng0Faa0B02CmdDeal.AimingControl.CMD_10 +
				              CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("lightModel"))) +
				              DuoCaiDeng0Faa0B02CmdDeal.AimingControl.CMD_12 +
				              CmdUtils.intToHexFour(Integer.parseInt(actionsMap.get("delayedTime"))) +
				              DuoCaiDeng0Faa0B02CmdDeal.AimingControl.CMD_14 +
				              CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("lighteness")));

		String cmd9 = CmdUtils.getDateLength(cmd10To15);

		String cmd3To15 = DuoCaiDeng0Faa0B02CmdDeal.AimingControl.CMD_3 +
				             DuoCaiDeng0Faa0B02CmdDeal.AimingControl.CMD_4 +
				             map.get("gatewayMac") +
				             map.get("deviceMac") +
				             DuoCaiDeng0Faa0B02CmdDeal.AimingControl.CMD_7 +
				             DuoCaiDeng0Faa0B02CmdDeal.AimingControl.CMD_8 +
				             cmd9 +
				             cmd10To15;

		String cmd2 = CmdUtils.getDateLength(cmd3To15);

		String cmd16 = CmdUtils.getCmdBodySum(cmd3To15);

		String fullCmd = DuoCaiDeng0Faa0B02CmdDeal.AimingControl.CMD_1 +
				          cmd2 + cmd3To15 + cmd16 +
				          DuoCaiDeng0Faa0B02CmdDeal.AimingControl.CMD_17;

		return fullCmd;
	}

	/**
	 * 获取调色控制命令
	 * @param map
	 * @return
	 */
	private String getColorControlCmd(Map<String, String> map){

		Map<String, String> actionsMap = (Map)JSONObject.parse(JSON.toJSONString(map.get("actions")));

		String cmd15 = CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("red"))) +
				        CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("green"))) +
				        CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("blue")));

		String cmd10To15 = DuoCaiDeng0Faa0B02CmdDeal.ColorControl.CMD_10 +
				              CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("lightModel"))) +
				              DuoCaiDeng0Faa0B02CmdDeal.ColorControl.CMD_12 +
				              CmdUtils.intToHexFour(5) +
				              DuoCaiDeng0Faa0B02CmdDeal.ColorControl.CMD_14 +
				              cmd15;

		String cmd9 = CmdUtils.getDateLength(cmd10To15);

		String cmd3To15 = DuoCaiDeng0Faa0B02CmdDeal.ColorControl.CMD_3 +
				             DuoCaiDeng0Faa0B02CmdDeal.ColorControl.CMD_4 +
				             map.get("gatewayMac") +
				             map.get("deviceMac") +
				             DuoCaiDeng0Faa0B02CmdDeal.ColorControl.CMD_7 +
				             DuoCaiDeng0Faa0B02CmdDeal.ColorControl.CMD_8 +
				             cmd9 +
				             cmd10To15;

		String cmd2 = CmdUtils.getDateLength(cmd3To15);

		String cmd16 = CmdUtils.getCmdBodySum(cmd3To15);

		String fullCmd = DuoCaiDeng0Faa0B02CmdDeal.ColorControl.CMD_1 +
				          cmd2 +
				          cmd3To15 +
				          cmd16 +
				          DuoCaiDeng0Faa0B02CmdDeal.ColorControl.CMD_17;

		return fullCmd;

	}


	/**
	 * 获取特定模式控制命令
	 * @param map
	 * @return
	 */
	private String getModeControlCmd(Map<String, String> map){

		Map<String, String> actionsMap = (Map)JSONObject.parse(JSON.toJSONString(map.get("actions")));

		String mode = actionsMap.get("mode");
		String cmd10 = "";
		String cmd11 = "";
		String modeValue = actionsMap.get("mode_value");

		if(DuoCaiDeng0Faa0B02CmdDeal.ModeControl.MODE_GRADUAL.equals(mode)){
			cmd10 = DuoCaiDeng0Faa0B02CmdDeal.ModeControl.CMD_10_GRADUAL;
			cmd11 = modeValue;
		}else if(DuoCaiDeng0Faa0B02CmdDeal.ModeControl.MODE_SALTUS_STEP.equals(mode)){
			cmd10 = DuoCaiDeng0Faa0B02CmdDeal.ModeControl.CMD_10_SALTUS_STEP;
			cmd11 = modeValue;
		}else if(DuoCaiDeng0Faa0B02CmdDeal.ModeControl.MODE_BREATH.equals(mode)){
			cmd10 = DuoCaiDeng0Faa0B02CmdDeal.ModeControl.CMD_10_BREATH;
			cmd11 = modeValue +
					 CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("red"))) +
					 CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("green"))) +
					 CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("blue")));
		}

		String cmd9 = CmdUtils.getDateLength(cmd10 + cmd11);

		String cmd3To11 = DuoCaiDeng0Faa0B02CmdDeal.ModeControl.CMD_3 +
				             DuoCaiDeng0Faa0B02CmdDeal.ModeControl.CMD_4 +
				             map.get("gatewayMac") +
				             map.get("deviceMac") +
				             DuoCaiDeng0Faa0B02CmdDeal.ModeControl.CMD_7 +
				             DuoCaiDeng0Faa0B02CmdDeal.ModeControl.CMD_8 +
				             cmd9 +
				             cmd10 +
				             cmd11;

		String cmd2 = CmdUtils.getDateLength(cmd3To11);

		String cmd12 = CmdUtils.getCmdBodySum(cmd3To11);

		String fullCmd = DuoCaiDeng0Faa0B02CmdDeal.ModeControl.CMD_1 +
						  cmd2 +
						  cmd3To11 +
						  cmd12 +
						  DuoCaiDeng0Faa0B02CmdDeal.ModeControl.CMD_13;

		return fullCmd;
	}


	/**
	 * 获取关灯控制命令
	 * @param map
	 * @return
	 */
	private String getCloseControlCmd(Map<String , String> map){

		String cmd10To12 = DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_10 +
				   			  DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_11 +
				   			  DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_12;

		String cmd9 = CmdUtils.getDateLength(cmd10To12);

		String cmd3To12 = DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_3 +
				  			 DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_4 +
				  			 map.get("gatewayMac") +
				  			 map.get("deviceMac") +
				  			 DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_7 +
				  			 DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_8 +
				  			 cmd9 +
				  			 cmd10To12;

		String cmd2 = CmdUtils.getDateLength(cmd3To12);

		String cmd13 = CmdUtils.getCmdBodySum(cmd3To12);

		String fullCmd = DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_1 +
						  cmd2 +
						  cmd3To12 +
						  cmd13 +
						  DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_14;

		return fullCmd;
	}

















}
